'''
python 调用lua 函数
# https://tool.4xseo.com/article/202945.html

'''
import lupa
import json
# 实例化一个lua解释器
lua = lupa.LuaRuntime()
aa = {
    "a": 5,
    "c": None,
    "b": ["a", "b"]
}
a = {
    "a": 5,
    "c": None,
    "b": 9,
    "d": aa
}
jsonchang = '''
     function(d)
         local t = {}
         for key, value in python.iterex(python.as_attrgetter(d).items()) do
             t[key] = value
            print(key,value,type(value))

         end
         t['d']['c'] = 'dfd'
         t['d']['b'] = {"Lua", "Tutorial"}
         return t
     end
    '''
lua_func = lua.eval(jsonchang)

r = lua_func(a)
print(r['c'])
print(r, type(r))  # 6

result = {}
for key, value in r.items():
    print(f'{key},{value}')
    result[key] = value
print(result, type(result))
print(result['d']['b'][0])
print(result['d']['b'][1])
# a = {
#     "a": 5,
#     "b": 9
# }
# select = '''
#     function (obj)
#         print("mytable type is  ",type(obj))
#         return obj["b"]
#     end
#     '''
# lua_func = lua.eval(select)

# r = lua_func(a)


# print(r, type(r))  # 6
